""" PsyBlockGame figure settings """

if __name__ == '__main__':
    import os
    import sys
    PATH_DIRS = [
        r'\bin\lib',
        r'\bin\lib\site-packages',
        r'\src',
    ]
    sys.path = [os.getcwd() + path for path in PATH_DIRS]

from PyQt4.QtCore import SIGNAL, Qt
from PyQt4.QtGui import *
from PsyBlockGame.Game import FigureConfig, Game, GameConfig
from PsyBlockGame.UI.FigureSettings import Ui_FigureSettings

class FigureSettings(QWidget):
    def __init__(self, config):
        super(FigureSettings, self).__init__()
        self.ui = Ui_FigureSettings()
        self.ui.setupUi(self)

        self.ui.preview.setRenderHint(QPainter.Antialiasing)
        self.ui.preview.setBackgroundBrush(self.palette().brush(QPalette.Window))

        self.config = config

        self.previewConfig = GameConfig()
        self.previewConfig.figures = [config]

        self.previewGame = Game(self.ui.preview, self.previewConfig)
        self.previewGame.startPreviewSingle()

        self.setPreviewSize(*self.getPreviewSize())

        self.updateElementByData()
        self.updatePreview(self.previewConfig)

    def setConfig(self, config):
        self.config = config
        self.previewConfig.figures[0] = config

    def updateElementByData(self):
        self.isInitData = True
        self.ui.figureType.setCurrentIndex(self.config.typeId)
        self.updateColorBtn()
        self.ui.figureKeyBtn.setText('  %s  ' % self.config.key if len(self.config.key) > 0 else ' ... ')
        self.isInitData = False

    def updateColorBtn(self):
        p = QPalette()
        p.setBrush(QPalette.Button, QBrush(self.config.color))
        self.ui.figureColorBtn.setPalette(p)

    def onFigureTypeChanged(self, typeId):
        if self.isInitData:
            return

        self.config.typeId = typeId
        self.updatePreview(self.previewConfig)
        self.setPreviewSize(*self.getPreviewSize())
        self.emit(SIGNAL('dataChanged()'))

    def onFigureKeyChange(self):
        w = QDialog(self)
        w.setWindowModality(Qt.WindowModal)
        w.setLayout(QVBoxLayout())
        w.layout().addWidget(QLabel(self.tr("Press any key or 'Esc' to cancel")))

        pressed = None
        def myKeyPress(e):
            nonlocal pressed
            if e.key() in (Qt.Key_Shift, Qt.Key_Alt, Qt.Key_Control):
                return
            try:
                pressed = QKeySequence(e.key()).toString()
            except:
                return
            w.close()
            
        w.keyPressEvent = myKeyPress
        
        w.exec_()
        if pressed is None:
            return
        
        self.ui.figureKeyBtn.setText('  %s  ' % pressed)
        self.config.key = pressed
        self.emit(SIGNAL('figureKeyChanged(QWidget *)'), self)
        self.emit(SIGNAL('dataChanged()'))

    def onColorChange(self):
        color = QColorDialog.getColor(self.config.color, self)
        if not color.isValid():
            return

        self.config.color = color
        self.updateColorBtn()
        self.updatePreview(self.previewConfig)
        self.emit(SIGNAL('dataChanged()'))

    def getPreviewSize(self):
        size = self.previewGame.scene.itemsBoundingRect().size()
        return int(size.width()), int(size.height())

    def setPreviewSize(self, w, h):
        w += 10; h += 10
        self.ui.preview.setMinimumSize(w, h)
        self.ui.preview.setMaximumSize(w, h)
        self.ui.preview.setSceneRect(self.previewGame.scene.itemsBoundingRect())

    def updatePreview(self, config):
        self.previewConfig.figureWidth          = config.figureWidth
        self.previewConfig.figureBorderWidth    = config.figureBorderWidth
        self.previewConfig.figureBackColor      = config.figureBackColor

        self.updateElementByData()
        self.previewGame.startPreviewSingle()

if __name__ == '__main__':
    qApp = QApplication(sys.argv)
    dlg = QDialog()
    dlg.setLayout(QGridLayout())
    fc = FigureConfig("Triangle")
    fc.key = 'A'
    dlg.layout().addWidget(FigureSettings(fc))
    
    sys.exit(dlg.exec_())
  